﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DBBase;
using System.Threading;
using System.Xml;
using System.Web;
using System.Runtime.InteropServices;
using System.IO;
using System.Data.OleDb;
using System.Data.SqlClient;

namespace Business
{
    public partial class huoqujcxmxx18c46 : Form
    {
        public static string sDataBaseIp;
        public static string sDataBaseName;
        public static string sDataBaseUserName;
        public static string sDataBasePwd;
        public static string sWebService = "";
        public static string sjyjgbh = "";
        public static string ssqm = "";
        private TextBox[] ctextbox;
        public static string[] cgjd;
        public huoqujcxmxx18c46()
        {
            InitializeComponent();
            
            ctextbox = new TextBox[4] {textBox3, textBox4, textBox5, textBox6 };
            cgjd = new string[4] { "clsslb", "clsslbmc", "xjyxmdh", "bzyjbj" };
            getjilu();
        }
       
        public string decode(string str)//utf8转中文
        {
            Encoding utf8 = Encoding.UTF8;
            // 首先用utf-8进行解码                    
            return HttpUtility.UrlDecode(str.ToUpper(), utf8);
        }
        public string encode(string str)//中文转utf8
        {
            Encoding utf8 = Encoding.UTF8;
            return HttpUtility.UrlEncode(str, utf8);
        }
        
        private void button4_Click(object sender, EventArgs e)
        {
           
        }
        public void lianwangchaxunjieyan()
        {
            string vehispara = "";
            string stoubao = "<?xml version=\"1.0\" encoding=\"GBK\"?>";
            vehispara = stoubao + string.Format("<root><QueryCondition><jyjgbh>{0}</jyjgbh><jylb>{1}</jylb></QueryCondition></root>",
                textBox1.Text,
                textBox2.Text);

            WebReference.Service m_sshenhe18C50 = new WebReference.Service();
            m_sshenhe18C50.Url = sWebService;
            string str = m_sshenhe18C50.queryObjectOut("18", m_mainform.sSQM, "18C46", vehispara);
         //   MessageBox.Show(decode(str));
            if (str.Length < 1)
            {
                MessageBox.Show("连接不到服务器");

            }
            else
            {
                XmlDocument xmlstr = new XmlDocument();
                xmlstr.LoadXml(str);
                if (xmlstr.SelectSingleNode("/root/head/code").InnerText == "1")
                {

                    for (int i = 0; i < 4; i++)
                    {
                        ctextbox[i].Text = decode(xmlstr.SelectSingleNode(string.Format(@"/root/body/vehispara/{0}", cgjd[i])).InnerText);
                    }

                    XmlNode xn = xmlstr.SelectSingleNode("/root/body");
                    // 得到根节点的所有子节点
                    XmlNodeList xnl = xn.ChildNodes;
                    foreach (XmlNode xn1 in xnl)
                    {
                        XmlElement xe = (XmlElement)xn1;
                        // 得到Type和ISBN两个属性的属性值

                        // 得到节点的所有子节点
                        XmlNodeList xnl0 = xe.ChildNodes;

                        var data1 = "";
                        var data2 = "";
                        var data3 = "";
                        var data4 = "";
                        data1 = decode(xnl0.Item(0).InnerText); ;
                        data2 = decode(xnl0.Item(1).InnerText);
                        data3 = decode(xnl0.Item(2).InnerText);
                        data4 = decode(xnl0.Item(3).InnerText);

                        Query objquery = new Query();
                        var sql = "";
                        if (objquery.selectDBcount(string.Format(@"select * from jcxmbiao where jcxm_jylb='{0}' and clsslb='{1}' and jczbh='{2}' ", textBox2.Text, data4,textBox1.Text)) > 0)
                        {
                            sql = string.Format(@"update jcxmbiao set clsslb='{0}',clsslbmc='{1}',xjyxmdh='{2}',bzyjbj='{3}' where jcxm_jylb='{4}' and jczbh='{5}'", data4, data3, data1, data2, textBox2.Text, textBox1.Text);
                        }
                        else
                        {
                            sql = string.Format(@"insert into jcxmbiao(jcxm_jylb,clsslb,clsslbmc,xjyxmdh,bzyjbj,jczbh) values('{0}','{1}','{2}','{3}','{4}','{5}')", textBox2.Text, data4, data3, data1, data2, textBox1.Text);
                        }
                        objquery.UpdateTable(sql);
                        getjilu();

                    }
                }
            }
        }

        private void but_add_Click(object sender, EventArgs e)
        {
            
        }
        public void insertdb()
        {
            Query objquery = new Query();
            string sql="";
            if (objquery.selectDBcount(string.Format(@"select * from jcxmbiao where jcxm_jylb='{0}'", textBox2.Text)) > 0)
            {
                sql = string.Format(@"update jcxmbiao set clsslb='{0}',clsslbmc='{1}',xjyxmdh='{2}',bzyjbj='{3}' where jcxm_jylb='{4}'", textBox3.Text, textBox4.Text, textBox5.Text, textBox6.Text, textBox2.Text);
            }
            else
            {
                sql = string.Format(@"insert into jcxmbiao(jcxm_jylb,clsslb,clsslbmc,xjyxmdh,bzyjbj) values('{0}','{1}','{2}','{3}','{4}')", textBox2.Text, textBox3.Text, textBox4.Text, textBox5.Text, textBox6.Text);
            }
            objquery.UpdateTable(sql);
            getjilu();
        }
        public void getjilu()
        {
            list.Rows.Clear();
            Query objquery = new Query();
            string ssqlex = "";
            ssqlex = string.Format(@"select * from jcxmbiao order by jcxm_jylb");
            DataTable dt = objquery.GetTable(ssqlex);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                list.Rows.Add();
                list.Rows[i].Cells[0].Value = dt.Rows[i]["jczbh"].ToString().Trim();
                list.Rows[i].Cells[1].Value = dt.Rows[i]["jcxm_jylb"].ToString().Trim();
                list.Rows[i].Cells[2].Value = dt.Rows[i]["clsslbmc"].ToString().Trim();
                list.Rows[i].Cells[3].Value = dt.Rows[i]["bzyjbj"].ToString().Trim();
                list.Rows[i].Cells[4].Value = dt.Rows[i]["xjyxmdh"].ToString().Trim();
            }
        }
        public MainForm m_mainform;
        private void but_download_Click(object sender, EventArgs e)
        {
            //判断是否核心功能
            if (m_mainform.shxgn.Contains("检验项目信息下载"))
            {
                m_mainform.baocunhxrj("检验项目信息下载", string.Format("检验项目信息下载"), "写入");// 核心功能日记  日记内容
            }
            lianwangchaxunjieyan();
        }

        private void but_save_Click(object sender, EventArgs e)
        {
            insertdb();
        }

        private void huoqujcxmxx18c46_Load(object sender, EventArgs e)
        {
            Query mquery = new Query();
            string ssql;
            ssql = string.Format(@"Select * From StationInfor ");

            sWebService = mquery.GetDBString(ssql, "SI_WEB");
            sjyjgbh = mquery.GetDBString(ssql, "SI_JCZBH");
            ssqm = mquery.GetDBString(ssql, "SI_SQM");

            //判断是否民警
            if (m_mainform.sfjy.Contains("检验项目信息下载"))
            {
                if (!m_mainform.SFMJ.Contains("是"))
                {
                    but_download.Enabled = false;
                }
            }
        }

    }
}
